package com.jf.yytgd.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jf.yytgd.entity.bean.User;
import com.jf.yytgd.dao.UserMapper;
import com.jf.yytgd.entity.dto.user.UserLoginDTO;
import com.jf.yytgd.entity.dto.user.UserRegisterDTO;
import com.jf.yytgd.service.UserService;
import jakarta.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;


/**
 * Created with IntelliJ IDEA
 * Description: 用户服务层实现
 * User:22954
 * Date:2025-01-13
 * Time:10:13
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
    @Resource
    private UserMapper userMapper;

    @Override
    public void saveUser(UserRegisterDTO userDTO) {
        User user = new User();
        BeanUtils.copyProperties(userDTO, user);
        userMapper.insertUser(user);
    }

    @Override
    public boolean login(UserLoginDTO userDTO) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", userDTO.getIdentifier())
                .or()
                .eq("phone", userDTO.getIdentifier())
                .or()
                .eq("email", userDTO.getIdentifier())
                .eq("password", userDTO.getPassword());
        User temp = userMapper.selectOne(queryWrapper);
        if (temp != null) {
            userDTO.setId(temp.getId());
            return true;
        } else {
            return false;
        }
    }
}
